﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using MaxStoreErp.Core.BaseClasses;
using MaxStoreShop.Domain.Directories;

namespace MaxStoreShop.Domain.Shop
{
    public class Debt : EntityBase, IEditableObject
    {
        Character _character;
        DateTime _debtDate;
        DateTime? _maturity;
        int _accruals;
        string _note;

        public int CharacterId { get; set; }
        
        /// <summary>
        /// Должник
        /// </summary>
        public virtual Character Character
        {
            get { return _character; }
            set { OnPropertyChanged(ref _character, value, "Character"); }
        }

        /// <summary>
        /// Дата выдачи долга
        /// </summary>
        [Required]
        public DateTime DebtDate
        {
            get { return _debtDate; }
            set { OnPropertyChanged(ref _debtDate, value, "DebtDate"); }
        }

        /// <summary>
        /// Дата возврата долга
        /// </summary>
        public DateTime? Maturity
        {
            get { return _maturity; }
            set { OnPropertyChanged(ref _maturity, value, "Maturity"); }
        }

        /// <summary>
        /// Размер долга
        /// </summary>
        [Required]
        public int Accruals
        {
            get { return _accruals; }
            set { OnPropertyChanged(ref _accruals, value, "Accruals"); }
        }

        /// <summary>
        /// Погашен ли долг
        /// </summary>
        public bool IsDebtReimbursement { get; set; }

        /// <summary>
        /// Примечание к долгу
        /// </summary>
        public string Note
        {
            get { return _note; }
            set { OnPropertyChanged(ref _note, value, "Note"); }
        }

        public int FormalizeDebtUserId { get; set; }

        /// <summary>
        /// Пользователь, оформивший долг
        /// </summary>
        public virtual StoreUser FormalizeDebtUser { get; set; }

        public int? AcquittanceDebtUserId { get; set; }
        /// <summary>
        /// Пользователь, погасивший долг
        /// </summary>
        public virtual StoreUser AcquittanceDebtUser { get; set; }

        #region IEditableObject

        private Debt _backupCopy;
        private bool _inEdit;

        public void BeginEdit()
        {
            if (_inEdit) return;
            _inEdit = true;
            _backupCopy = MemberwiseClone() as Debt;
        }

        public void CancelEdit()
        {
            if (!_inEdit)
                return;

            _inEdit = false;
            CharacterId = _backupCopy.CharacterId;
            Character = _backupCopy.Character;
            DebtDate = _backupCopy.DebtDate;
            Maturity = _backupCopy.Maturity;
            Accruals = _backupCopy.Accruals;
            IsDebtReimbursement = _backupCopy.IsDebtReimbursement;
            Note = _backupCopy.Note;
            FormalizeDebtUserId = _backupCopy.FormalizeDebtUserId;
            FormalizeDebtUser = _backupCopy.FormalizeDebtUser;
            AcquittanceDebtUserId = _backupCopy.AcquittanceDebtUserId;
            AcquittanceDebtUser = _backupCopy.AcquittanceDebtUser;
        }

        public void EndEdit()
        {
            if (!_inEdit) return;
            _inEdit = false;
            _backupCopy = null;
        }
        #endregion
    }
}
